package com.hghivln.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.blog.common.lang.Result;
import com.hghivln.blog.entity.Blog;
import com.hghivln.blog.entity.BlogComment;
import com.hghivln.blog.mapper.BlogCommentMapper;
import com.hghivln.blog.mapper.BlogMapper;
import com.hghivln.blog.pojo.dto.BlogCommentDto;
import com.hghivln.blog.pojo.vo.BlogCommentVo;
import com.hghivln.blog.service.BlogCommentService;
import com.hghivln.blog.shiro.LoginUser;
import com.hghivln.blog.utils.IdWorker;
import com.hghivln.blog.utils.PagesUtils;
import com.hghivln.blog.utils.ShiroUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 博客评论表 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-09-25
 */
@Service
public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogComment> implements BlogCommentService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public Result<?> getByPage(BlogCommentDto blogCommentDto) {
        QueryWrapper<BlogComment> qw = new QueryWrapper<>();
        Long userId = ShiroUtils.getLoginUser().getId();
        qw.like(userId != null, "user_id", userId);
        qw.eq("deleted",0);
        qw.orderByDesc("created_time");
        // 根据用户id查询所有博客评论
        List<BlogComment> blogCommentList = getBaseMapper().selectList(qw);
        if (!CollectionUtils.isEmpty(blogCommentList)) {
            List<Long> blogIds = blogCommentList.stream().map(BlogComment::getBlogId).collect(Collectors.toList());
            // 同一个用户在同一条微博下可能有多个评论  先去重
            blogIds = new ArrayList<>(new HashSet<>(blogIds));
            List<Blog> blogList = blogMapper.selectBatchIds(blogIds);
            // 封装博客 和 评论内容
            List<BlogCommentVo> voList = blogCommentList.stream().map(e -> {  //遍历所有的博客评论
                BlogCommentVo blogCommentVo = new BlogCommentVo();
                blogCommentVo.setId(e.getId());
                blogCommentVo.setContent(e.getContent());
                // 根据博客评论 的博客id 找到博客
                Blog blog = blogList.stream().filter(b -> e.getBlogId().equals(b.getBlogId())).findFirst().orElse(new Blog());
                BeanUtils.copyProperties(blog, blogCommentVo);
                return blogCommentVo;
            }).collect(Collectors.toList());
            Page pages = PagesUtils.getPages(blogCommentDto.getPage(), blogCommentDto.getLimit(), voList);
            return new Result<>(pages.getTotal(),pages.getRecords());
        } else {
            return new Result<>(0L, new ArrayList<>(0));
        }
    }

    @Override
    public void saveComment(BlogComment blogComment) {
        LoginUser user = ShiroUtils.getLoginUser();
        blogComment.setId(idWorker.nextId());
        blogComment.setUserId(user.getId());
        blogComment.setHeader(user.getHeader());
        blogComment.setNickname(user.getNickname());
        getBaseMapper().insert(blogComment);

        // 根据博客id查询博客
        Blog blog = blogMapper.selectById(blogComment.getBlogId());
        // 评论数 +1
        blog.setBlogComment(blog.getBlogComment()+1);
        blogMapper.updateById(blog);

    }

    @Override
    public Result<?> getByBlogId(BlogCommentDto blogCommentDto) {
        Page<BlogComment> page = new Page<>(blogCommentDto.getPage(), blogCommentDto.getLimit());
        QueryWrapper<BlogComment> qw = new QueryWrapper<>();
        qw.eq("blog_id",blogCommentDto.getBlogId());
        qw.eq("deleted",0);
        qw.orderByDesc("created_time");
        Page<BlogComment> pages = getBaseMapper().selectPage(page, qw);
        return new Result<>(pages.getTotal(),pages.getRecords());
    }

    @Override
    public void deleteComment(Long id) {
        getBaseMapper().deleteComment(id);
    }


}


